/**
 * 环境配置
 * 统一管理不同环境的配置
 */

// 环境类型
export type EnvType = 'development' | 'staging' | 'production'

// 基础配置接口
interface BaseConfig {
  app: {
    title: string
    version: string
    env: EnvType
    port: number
    host: string
  }
  api: {
    baseUrl: string
    timeout: number
  }
  features: {
    mock: boolean
    debug: boolean
    analytics: boolean
    devtools: boolean
    hotReload: boolean
    sourcemap: boolean
  }
}

// 开发环境配置
const developmentConfig: BaseConfig = {
  app: {
    title: 'Convallaria企业级前端框架-开发环境',
    version: '1.0.0',
    env: 'development',
    port: 3000,
    host: '0.0.0.0'
  },
  api: {
    baseUrl: 'http://localhost:18000',
    timeout: 10000
  },
  features: {
    mock: true,
    debug: true,
    analytics: false,
    devtools: true,
    hotReload: true,
    sourcemap: true
  }
}

// 预发布环境配置
const stagingConfig: BaseConfig = {
  app: {
    title: 'Convallaria企业级前端框架-预发布环境',
    version: '1.0.0',
    env: 'staging',
    port: 3000,
    host: '0.0.0.0'
  },
  api: {
    baseUrl: 'https://staging-api.convallaria.com',
    timeout: 10000
  },
  features: {
    mock: false,
    debug: true,
    analytics: true,
    devtools: true,
    hotReload: false,
    sourcemap: true
  }
}

// 生产环境配置
const productionConfig: BaseConfig = {
  app: {
    title: 'Convallaria企业级前端框架',
    version: '1.0.0',
    env: 'production',
    port: 3000,
    host: '0.0.0.0'
  },
  api: {
    baseUrl: 'https://api.convallaria.com',
    timeout: 10000
  },
  features: {
    mock: false,
    debug: false,
    analytics: true,
    devtools: false,
    hotReload: false,
    sourcemap: false
  }
}

// 配置映射
const configMap = {
  development: developmentConfig,
  staging: stagingConfig,
  production: productionConfig
}

// 获取当前环境
export const getCurrentEnv = (): EnvType => {
  // 在 Vite 配置文件中，import.meta.env 可能不可用
  if (typeof import.meta !== 'undefined' && import.meta.env) {
    const env = import.meta.env.VITE_APP_ENV as EnvType
    return env || 'development'
  }
  // 回退到 process.env 或默认值
  return (process.env.VITE_APP_ENV as EnvType) || 'development'
}

// 获取环境配置
export const getEnvConfig = (): BaseConfig => {
  const env = getCurrentEnv()
  return configMap[env] || developmentConfig
}

// 检查是否为开发环境
export const isDevelopment = (): boolean => {
  return getCurrentEnv() === 'development'
}

// 检查是否为生产环境
export const isProduction = (): boolean => {
  return getCurrentEnv() === 'production'
}

// 检查是否为预发布环境
export const isStaging = (): boolean => {
  return getCurrentEnv() === 'staging'
}

// 获取API基础URL
export const getApiBaseUrl = (): string => {
  return getEnvConfig().api.baseUrl
}

// 获取应用标题
export const getAppTitle = (): string => {
  return getEnvConfig().app.title
}

// 获取应用版本
export const getAppVersion = (): string => {
  return getEnvConfig().app.version
}

// 检查功能是否启用
export const isFeatureEnabled = (feature: keyof BaseConfig['features']): boolean => {
  return getEnvConfig().features[feature]
}

// 导出当前配置
export const envConfig = getEnvConfig()
